<?php
namespace Api\Controller;

class HomeController extends ApiController
{
    // 首页数据
    public function homepage()
    {
        $M_product = M('product');
        $M_product_image = M('product_image');
        $M_ad=M('ad');
        $fields = array(
            'id product_id',
            'title',
            'type',
            'slogan_title'
        );
        
        // 今日头条
        $list_top = $M_product->where(array(
            'is_top' => 1
        ))
            ->order('update_time DESC')
            ->field($fields)
            ->limit(0, 5)
            ->select();
        foreach ($list_top as &$item) {
            $image_info = $M_product_image->where(array(
                'product_id' => $item['product_id']
            ))
                ->order('id ASC')
                ->find();
            $item['image'] = get_url($image_info['cover_id']);
        }
        // 拍品
        $list_paipin = $M_product->where(array(
            'type' => 'paipin',
            'is_home_show' => 1
        ))
            ->order('update_time DESC')
            ->field($fields)
            ->limit(0, 4)
            ->select();
        
        foreach ($list_paipin as &$item) {
            $image_info = $M_product_image->where(array(
                'product_id' => $item['product_id']
            ))
                ->order('id ASC')
                ->find();
            
            $item['image'] = get_url($image_info['cover_id'], 'path');
        }
        
        // 抢购会
        $map['begin_time']=array('elt',NOW_TIME);
        $map['end_time']=array('egt',NOW_TIME);
        $map['position']=array('eq','panic_banner');
        $list=M('ad')->field(array('id','cover_id','product_id'))->where($map)->order('sort desc')->select();
        $list_panic=array();
        foreach ($list as $k=>$v){
            $list_panic[$k]['id']=$v['id'];
            $list_panic[$k]['product_id']=$v['product_id'];
            $list_panic[$k]['image']=get_cover($v['cover_id'], 'path') ;
        }
        // 选礼物
        $list_gift = $M_product->where(array(
            'type' => 'putong',
            'is_home_show' => 1,
            'in_special_gift' => 1
        ))
            ->order('update_time DESC')
            ->field($fields)
            ->limit(0, 4)
            ->select();
        foreach ($list_gift as &$item) {
            $image_info = $M_product_image->where(array(
                'product_id' => $item['product_id']
            ))
                ->order('id ASC')
                ->find();
            
            $item['image'] = get_url($image_info['cover_id'], 'path');
        }
        
        $result = array();
        $result['top_products'] = $list_top;
        $result['paipin_products'] = $list_paipin;
        $result['panic_products'] = $list_panic;
        $result['gift_products'] = $list_gift;
        
        response(1, $result);
    }
}
?>